package com.zhentao.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.file.service.Impl.FileStorageServiceImpl;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.article.pojo.ApArticleContent;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.service.ApArticleContentService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 桃
 */
@Service
public class ApArticleContentServiceImpl extends ServiceImpl<ApArticleContentMapper, ApArticleContent>
        implements ApArticleContentService {

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageServiceImpl fileStorageService;
    @Autowired
    private ArticleMapper articleMapper;


    @Async  //开启异步
    @SneakyThrows  //异常抛出
    @Override
    public ResponseResult createStaticUrl(Long articleId) {
        //根据iD生成html静态文件，然后文件上传到minio里面
        //1.根据id获取文章内容
//        LambdaQueryWrapper<ApArticleContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(ApArticleContent::getArticleId,articleId);

        ApArticleContent apArticleContent = apArticleContentMapper.selectOne(new LambdaQueryWrapper<ApArticleContent>()
                .eq(ApArticleContent::getArticleId, articleId));

        if (apArticleContent != null) {
            //2.把内容通过freemarker生产html文件
            Template template = configuration.getTemplate("article.ftl");
            Map<String, Object> map = new HashMap<>();
            map.put("content", JSONArray.parseArray(apArticleContent.getContent()));

            //3.把html上传到minio里面 运用stream流
            String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            ByteArrayInputStream stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));

            String url = fileStorageService.uploadHtmlFile(apArticleContent.getArticleId() + ".html", stream);

            //4.修改ap_article表里面的static_url字段
            ApArticle article = new ApArticle();
            article.setId(articleId);
            article.setStaticUrl(url);
            articleMapper.updateById(article);


            return ResponseResult.okResult(200, url);
        }


        return ResponseResult.errorResult(201, "文章不存在");
    }
}
